package cn.xuchunh.campusbbs.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.xuchunh.campusbbs.R;
import cn.xuchunh.campusbbs.base.BaseRecyclerViewAdapter;
import cn.xuchunh.campusbbs.bean.Reply;
import cn.xuchunh.campusbbs.bean.ReplyContent;
import cn.xuchunh.campusbbs.bean.Topic;
import cn.xuchunh.campusbbs.ui.activities.UserActivity;
import cn.xuchunh.campusbbs.utils.DateUtil;
import cn.xuchunh.campusbbs.viewholder.TopicReplyViewHolder;

/**
 * Created by XuChunH on 2015/9/14.
 */
public class ReplyListAdapter2 extends BaseRecyclerViewAdapter<TopicReplyViewHolder, Reply> {

    private String userFormat, positionFormat, replyFormat;

    public ReplyListAdapter2(Context context, Topic topic) {
        super(context);
        dataList.addDataList(topic.getReplyList());
        setNeedFooter(true);
        Resources resources = context.getResources();
        userFormat = resources.getString(R.string.format_topic_reply_user);
        positionFormat = resources.getString(R.string.format_topic_reply_position);
        replyFormat = resources.getString(R.string.format_topic_reply);
    }

    @Override
    public TopicReplyViewHolder onCreateContentViewHolder(ViewGroup parent, int viewType) {
        return new TopicReplyViewHolder(inflater.inflate(R.layout.item_topic_detail, parent, false));
    }


    @Override
    protected void onBindContentViewHolder(TopicReplyViewHolder holder, int position) {
        Reply reply = dataList.get(position);
        holder.user.setText(String.format(userFormat, reply.getName(), reply.getUserTitle()));
        holder.avatar.setImageURI(Uri.parse(reply.getAvatar()));
        holder.avatar.setOnClickListener(this);
        holder.avatar.setTag(position);
        holder.date.setText(DateUtil.formatDate(reply.getPostsDate()));
        if (reply.getPosition() == 1) {
            holder.position.setText(R.string.topic_author);
        } else {
            holder.position.setText(String.format(positionFormat, reply.getPosition()));
        }
        String quote = "";
        if (reply.isQuote()) {
            Matcher matcher = Pattern.compile("\\b[_0-9a-zA-Z\\u4E00-\\u9FA5]+\\b")
                    .matcher(reply.getQuoteContent());
            if (matcher.find()) {
                quote = String.format(replyFormat, matcher.group());
            } else {
                quote = String.format(replyFormat, reply.getQuoteUserName());
            }
        }

        holder.content.setContent(reply.getContent());

    }

    @Override
    public void onViewRecycled(RecyclerView.ViewHolder holder) {
        if (holder instanceof TopicReplyViewHolder) {
            ((TopicReplyViewHolder) holder).content.removeAllViews();
        }
        super.onViewRecycled(holder);

    }

    public void setTopic(Topic topic) {
        dataList.clear();
        dataList.addDataList(topic.getReplyList());
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.avatar && v.getTag() != null){
            int position = (int) v.getTag();
            UserActivity.launch(getContext(), getItem(position).getUserId());
        }else {
            super.onClick(v);
        }
    }
}
